package cn.wolfcode.trip.admin.controller;


import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.query.TravelCommendQueryObject;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/travelCommend")
public class TravelCommendController {

    @Autowired
    private ITravelCommendService travelCommendService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") TravelCommendQueryObject qo, Model model) {
        //设置推荐时间的排序
        qo.setOrderBy("schedule desc");
        model.addAttribute("result", travelCommendService.selectForList(qo));
        return "travelCommend/list";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(TravelCommend travelCommend, MultipartFile pic) {
        //判断传过来的文件数据
        if (pic!=null && pic.getContentType().startsWith("image") && pic.getSize()>0){
            //设置封面的值
            String url = UploadUtil.upload(pic);
            travelCommend.setCoverUrl(url);
        }
        travelCommendService.saveOrUpdate(travelCommend);
        return "redirect:/travelCommend/list.do";
    }


}
